# %%
import os

# os.environ['OPENAI_API_KEY'] = ''
# %% use env
from dotenv import load_dotenv

load_dotenv()

from langchain.prompts import PromptTemplate
from langchain_community.llms import OpenAI
# %% Comma Separated List
from langchain.output_parsers import CommaSeparatedListOutputParser

# Creating an object of CommaSeparatedListOutputParser
# 指定模型输出 逗号分割的列表: 1. xxx \n2. xxx\n
# #%% JSON format
# ...
output_parser = CommaSeparatedListOutputParser()
format_instructions = output_parser.get_format_instructions()

# Your response should be a list of comma separated values, eg: `foo, bar, baz`
print(format_instructions)
# %%
prompt = PromptTemplate(
    template="Provide 5 examples of {query}.\n{format_instructions}",
    input_variables=['query'],
    partial_variables={'format_instructions': format_instructions}
)

# llm = OpenAI(temperature=0.9, model="gpt-3.5-turbo-instruct")
llm = OpenAI(temperature=0.9)

prompt = prompt.format(query="Currencies")  # Currencies: 货币实例
print(prompt)

output = llm(prompt)
print(output)

# %% JSON format
from langchain.output_parsers import StructuredOutputParser, ResponseSchema

# 指定返回什么
'''json
{
    'currency': '',
    'abbrevation': ''
}
'''
response_schema = [
    ResponseSchema(name='currency', description="answer to the user's question"),
    # abbrevation: 缩写
    ResponseSchema(name='abbrevation', description="What's the abbrevation of that currency"),
]

output_parser = StructuredOutputParser.from_response_schemas(response_schema)
print(output_parser)

format_instructions = output_parser.get_format_instructions()
print(format_instructions)

prompt = PromptTemplate(
    template="answer the users question as best as possible.\n{format_instructions}\n{query}",
    input_variables=['query'],  # 用户输入
    partial_variables={'format_instructions': format_instructions}  # 指定格式的输入语句
)
print(prompt)

prompt = prompt.format(query='what is the currency of india?')
print(llm(prompt))
'''
```json
{
	"currency": "Indian rupee",
	"abbrevation": "INR"
}
```
'''
